package com.dubbo.controller;

import com.dubbo.common.check.CheckUtil;
import com.dubbo.common.redis.RedisDao;
import com.dubbo.common.result.ResultInfo;
import com.dubbo.common.security.SecurityUtils;
import com.dubbo.domain.User;
import com.dubbo.domain.info.ResultMessage;
import com.dubbo.service.business.my.MyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 我的
 * @author lihongwu
 * 2018年12月15日 下午3:00:51
 */
@Controller
@RequestMapping("/my")
public class MyController {
	
	@Autowired
	private RedisDao redisDao;
	@Autowired
	private MyService myService;
	
	/**
	 * 我的补助金(只有电工才能访问此接口)
	 *
	 * @author lihongwu
	 * 2018年12月15日 下午3:18:17
	 * @param request
	 * @return
	 * @throws Exception 
	 */
	@PostMapping("/myInstallSubsidy")
	@ResponseBody
	public Map<String,Object> myInstallSubsidy(HttpServletRequest request) throws Exception{
		User user = (User) CheckUtil.checkOline3(request);
		SecurityUtils.checkRole(ResultMessage.ROLE_ELECTRIC, redisDao, request.getHeader(ResultInfo.USERTOKEN));
		return myService.myInstallSubsidy(user);
	}
	
	/**
	 * 安装补助金记录(只有电工才能访问此接口)
	 *
	 * @author lihongwu
	 * 2018年12月15日 下午4:06:32
	 * @param request
	 * @param pageStart
	 * @param pageTotal
	 * @return
	 * @throws Exception
	 */
	@PostMapping("/installSubsidyDetail")
	@ResponseBody
	public Map<String,Object> installSubsidyDetail(HttpServletRequest request,Long pageStart,Long pageTotal) throws Exception{
		User user = (User) CheckUtil.checkOline3(request);
		SecurityUtils.checkRole(ResultMessage.ROLE_ELECTRIC, redisDao, request.getHeader(ResultInfo.USERTOKEN));
		return myService.installSubsidyDetail(user,pageStart,pageTotal);
	}
	
	/**
	 * 提现记录
	 *
	 * @author lihongwu
	 * 2018年12月15日 下午5:02:50
	 * @param request
	 * @param pageStart
	 * @param pageTotal
	 * @return
	 * @throws Exception
	 */
	@PostMapping("/drawithDetail")
	@ResponseBody
	public Map<String,Object> drawithDetail(HttpServletRequest request,Long pageStart,Long pageTotal) throws Exception{
		User user = (User) CheckUtil.checkOline3(request);
		//SecurityUtils.checkRole(ResultMessage.ROLE_ELECTRIC, redisDao, request.getHeader(ResultInfo.USERTOKEN));
		return myService.drawithDetail(user,pageStart,pageTotal);
	}
	
	/**
	 * 我的电工
	 * @author gujiahua
	 * 2018年12月17日 上午11:09:15
	 * @param request
	 * @param pageStart
	 * @param pageTotal
	 * @param keyWord
	 * @return
	 * @throws Exception
	 */
	@PostMapping("/electricians.wx")
	@ResponseBody
	public Map<String,Object> getElectriciansList(HttpServletRequest request,Long pageStart,Long pageTotal,String keyWord) throws Exception{
		User user = (User) CheckUtil.checkOline3(request);
//		SecurityUtils.checkRole(ResultMessage.ROLE_TRADER, redisDao, request.getHeader(ResultInfo.USERTOKEN));
		return myService.getElectriciansList(user,pageStart,pageTotal,keyWord);
	}
	
	
	/**
	 * 我的分销商
	 * @author gujiahua
	 * 2018年12月17日 上午11:50:05
	 * @param request
	 * @param pageStart
	 * @param pageTotal
	 * @param keyWord
	 * @return
	 * @throws Exception
	 */
	@PostMapping("/distributors.wx")
	@ResponseBody
	public Map<String,Object> getDistributorsList(HttpServletRequest request,Long pageStart,Long pageTotal,String keyWord) throws Exception{
		User user = (User) CheckUtil.checkOline3(request);
//		SecurityUtils.checkRole(ResultMessage.ROLE_AGENT, redisDao, request.getHeader(ResultInfo.USERTOKEN));
		return myService.getDistributorsList(user,pageStart,pageTotal,keyWord);
	}
	
	/**
	 * 获取推荐二维码 (代理商，分销商，电工都通过这个接口获取推荐二维码)
	 *
	 * @author lihongwu
	 * 2018年12月15日 下午5:26:14
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@PostMapping("/getCode")
	@ResponseBody
	public Map<String,Object> getCode(HttpServletRequest request) throws Exception{
		User user = (User) CheckUtil.checkOline3(request);
		//SecurityUtils.checkRole(ResultMessage.ROLE_ELECTRIC, redisDao, request.getHeader(ResultInfo.USERTOKEN));
		return myService.getCode(user);
	}
	
	/**
	 * 获取个人信息
	 * @author gujiahua
	 * 2018年12月18日 上午11:07:03
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@PostMapping("/getInfo.wx")
	@ResponseBody
	public Map<String,Object> getInformation(HttpServletRequest request) throws Exception{
		User user = (User) CheckUtil.checkOline3(request);
		//SecurityUtils.checkRole(ResultMessage.ROLE_ELECTRIC, redisDao, request.getHeader(ResultInfo.USERTOKEN));
		return myService.getInformation(user);
	}
	
	/**
	 * 修改头像
	 * @author gujiahua
	 * 2018年12月18日 上午11:28:29
	 * @param request
	 * @param picId
	 * @return
	 * @throws Exception
	 */
	@PostMapping("/updateHeader.wx")
	@ResponseBody
	public Map<String,Object> updateHeaderImg(HttpServletRequest request,String picId) throws Exception{
		User user = (User) CheckUtil.checkOline3(request);
		//SecurityUtils.checkRole(ResultMessage.ROLE_ELECTRIC, redisDao, request.getHeader(ResultInfo.USERTOKEN));
		return myService.updateHeaderImg(user,picId);
	}
	
	/**
	 * 获取用户消息
	 * @author gujiahua
	 * 2018年12月18日 上午11:54:44
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@PostMapping("/getMsg.wx")
	@ResponseBody
	public Map<String,Object> getUserMessageList(HttpServletRequest request,Long pageStart,Long pageTotal) throws Exception{
		User user = (User) CheckUtil.checkOline3(request);
		//SecurityUtils.checkRole(ResultMessage.ROLE_ELECTRIC, redisDao, request.getHeader(ResultInfo.USERTOKEN));
		return myService.getUserMessageList(user,pageStart,pageTotal);
	}
	
	/**
	 * 获取用户未读消息数量
	 *
	 * @author lihongwu
	 * 2019年2月28日 上午9:47:41
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@PostMapping("/getUserNoReadMessageCount")
	@ResponseBody
	public Map<String,Object> getUserNoReadMessageCount(HttpServletRequest request) throws Exception{
			User user = (User) CheckUtil.checkOline3(request);
		//SecurityUtils.checkRole(ResultMessage.ROLE_ELECTRIC, redisDao, request.getHeader(ResultInfo.USERTOKEN));
		return myService.getUserNoReadMessageCount(user);
	}
	
	/**
	 * 用户读消息(将用户消息全部设置成已读)
	 *
	 * @author lihongwu
	 * 2019年2月28日 上午9:47:41
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@PostMapping("/readMessage")
	@ResponseBody
	public Map<String,Object> readMessage(HttpServletRequest request) throws Exception{
		User user = (User) CheckUtil.checkOline3(request);
		//SecurityUtils.checkRole(ResultMessage.ROLE_ELECTRIC, redisDao, request.getHeader(ResultInfo.USERTOKEN));
		return myService.readMessage(user);
	}
	
	/**
	 * 扫码获取到userId之后调用的接口
		若普通用户扫代理商和分销商，直接成为电工
		若分销商和代理商扫普通用户或代理商扫电工，则页面显示邀请按钮
	 *
	 * @author lihongwu
	 * 2018年12月24日 上午11:38:58
	 * @param request
	 * @param codeUserId
	 * @return
	 * @throws Exception
	 */
	@PostMapping("/sweepCode.wx")
	@ResponseBody
	public Map<String,Object> sweepCode(HttpServletRequest request,String codeUserId) throws Exception{
		User user = (User) CheckUtil.checkOline3(request);
		//SecurityUtils.checkRole(ResultMessage.ROLE_ELECTRIC, redisDao, request.getHeader(ResultInfo.USERTOKEN));
		return myService.sweepCode(user,codeUserId);
	}
	
	/**
	 * sweepCode的后续接口 , 点击邀请类型按钮之后访问   例如：代理商邀请普通用户，有电工和分销商两个按钮，点击按钮后访问该接口
	 *
	 * @author lihongwu
	 * 2018年12月24日 下午5:08:37
	 * @param request
	 * @param codeUserId
	 * @param type  1=邀请成为电工,2=邀请成为分销商
	 * @return
	 * @throws Exception
	 */
	@PostMapping("/sweepCodeAfter.wx")
	@ResponseBody
	public Map<String,Object> sweepCodeAfter(HttpServletRequest request,String codeUserId,Integer type) throws Exception{
		User user = (User) CheckUtil.checkOline3(request);
		//SecurityUtils.checkRole(ResultMessage.ROLE_ELECTRIC, redisDao, request.getHeader(ResultInfo.USERTOKEN));
		return myService.sweepCodeAfter(user,codeUserId,type);
	}
	
	/**
	 * 提交反馈
	 * @author wujunyuan
	 * @date 2018年12月28日  上午10:12:48
	 * @param request
	 * @param picIds 图片数组
	 * @param content 意见内容
	 * @return
	 * @throws Exception
	 */
	@PostMapping("/option.wx")
	@ResponseBody
	public Map<String,Object> option(HttpServletRequest request,String picIds,String content) throws Exception{
		User user = (User) CheckUtil.checkOline3(request);
		//SecurityUtils.checkRole(ResultMessage.ROLE_ELECTRIC, redisDao, request.getHeader(ResultInfo.USERTOKEN));
		return myService.option(user,picIds,content);
	}
	
	/**
	 * 根据电工id获取电工信息
	 * @author wujunyuan
	 * @date 2018年12月28日  上午10:12:48
	 * @param request
	 * @param userId
	 * @return
	 * @throws Exception
	 */
	@PostMapping("/getUserMsg.wx")
	@ResponseBody
	public Map<String,Object> getUserMsg(HttpServletRequest request,String userId) throws Exception{
		User user = (User) CheckUtil.checkOline3(request);
		//SecurityUtils.checkRole(ResultMessage.ROLE_ELECTRIC, redisDao, request.getHeader(ResultInfo.USERTOKEN));
		return myService.getUserMsg(user,userId);
	}
	
	/**
	 * 联系客服
	 * @author wujunyuan
	 * @date 2019年1月3日  上午11:14:36
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@PostMapping("/call.wx")
	@ResponseBody
	public Map<String,Object> call(HttpServletRequest request) throws Exception{
		User user = (User) CheckUtil.checkOline3(request);
		//SecurityUtils.checkRole(ResultMessage.ROLE_ELECTRIC, redisDao, request.getHeader(ResultInfo.USERTOKEN));
		return myService.getServicePhone(user);
	}
	
	/**
	 * 查看订单物流
	 * @author chenluqiang
	 * @date 2019年1月3日 下午4:50:02 
	 * @param request
	 * @param orderId 订单id
	 * @return
	 * @throws Exception
	 */
	@PostMapping("/kuaidi.wx")
	@ResponseBody
	public Map<String,Object> kuaidi(HttpServletRequest request,String orderId) throws Exception{
		User user = (User) CheckUtil.checkOline3(request);
		//校验权限
		SecurityUtils.checkPermission("sys:shipping:query", redisDao, request.getHeader(ResultInfo.USERTOKEN));
		return myService.getKuaidi(user,orderId);
	}
	
	/**
	 * 更新店名+具体地址
	 * @author wujunyuan
	 * @date 2019年1月4日  上午9:55:52
	 * @param request
	 * @param shopName
	 * @return
	 * @throws Exception
	 */
	@PostMapping("/shopName.wx")
	@ResponseBody
	public Map<String,Object> shopName(HttpServletRequest request,String shopName,String shopAddress) throws Exception{
		User user = (User) CheckUtil.checkOline3(request);
		//SecurityUtils.checkRole(ResultMessage.ROLE_ELECTRIC, redisDao, request.getHeader(ResultInfo.USERTOKEN));
		return myService.editShopName(user,shopName,shopAddress);
	}
	
	/**
	 * 获取上级电话
	 *
	 * @author lihongwu
	 * 2019年1月18日 下午4:58:00
	 * @param request
	 * @param shopName
	 * @return
	 * @throws Exception
	 */
	@PostMapping("/superiorPhone.wx")
	@ResponseBody
	public Map<String,Object> superiorPhone(HttpServletRequest request) throws Exception{
		User user = (User) CheckUtil.checkOline3(request);
		//SecurityUtils.checkRole(ResultMessage.ROLE_ELECTRIC, redisDao, request.getHeader(ResultInfo.USERTOKEN));
		return myService.superiorPhone(user);
	}
	
	/**
	 * 绑定微信(存openId,wxName)
	 *
	 * @author lihongwu
	 * 2019年2月22日 上午11:07:19
	 * @param request
	 * @param openId
	 * @param wxName 微信昵称
	 * @return
	 * @throws Exception
	 */
	@PostMapping("/bindWx")
	@ResponseBody
	public Map<String,Object> bindWx(HttpServletRequest request,String openId,String wxName) throws Exception{
		User user = (User) CheckUtil.checkOline3(request);
		//SecurityUtils.checkRole(ResultMessage.ROLE_ELECTRIC, redisDao, request.getHeader(ResultInfo.USERTOKEN));
		return myService.bindWx(user,openId,wxName);
	}
	
	/**
	 * 获取账号绑定的微信号信息
	 *
	 * @author lihongwu
	 * 2019年2月22日 上午11:14:47
	 * @param request
	 * @return
	 * @throws Exception 
	 */
	@PostMapping("/getBindWx")
	@ResponseBody
	public Map<String,Object> getBindWx(HttpServletRequest request) throws Exception{
		User user = (User) CheckUtil.checkOline3(request);
		return myService.getBindWx(user);
	}
	
	/**
	 * 设置用户极光推送所需的设备注册id
	 *
	 * @author lihongwu
	 * 2019年3月1日 上午11:56:45
	 * @param request
	 * @param registrationId
	 * @return
	 * @throws Exception
	 */
	@PostMapping("/setRegistrationId")
	@ResponseBody
	public Map<String,Object> setRegistrationId(HttpServletRequest request,String registrationId) throws Exception{
		User user = (User) CheckUtil.checkOline3(request);
		return myService.setRegistrationId(user,registrationId);
	}
	
	/**
	 * 保存用户信息
	 * @author chenluqiang
	 * @date 2019年4月1日 下午3:09:36 
	 * @param request
	 * @param picId
	 * @param shopName
	 * @param shopAddress
	 * @return
	 * @throws Exception
	 */
	@PostMapping("/save.wx")
	@ResponseBody
	public Map<String,Object> save(HttpServletRequest request,String picId,String shopName,String shopAddress) throws Exception{
		User user = (User) CheckUtil.checkOline3(request);
		//SecurityUtils.checkRole(ResultMessage.ROLE_ELECTRIC, redisDao, request.getHeader(ResultInfo.USERTOKEN));
		return myService.save(user,picId,shopName,shopAddress);
	}
	
	/**
	 * 根据用户id获取用户信息
	 * @author chenluqiang
	 * @date 2019年4月1日 下午3:23:23 
	 * @param request
	 * @param userId
	 * @return
	 * @throws Exception
	 */
	@PostMapping("/getInfoById.wx")
	@ResponseBody
	public Map<String,Object> getInfoById(HttpServletRequest request,String userId) throws Exception{
		CheckUtil.checkOline3(request);
		return myService.getInfoById(userId);
	}
}
